<template>
  <div class="content">
    <div class="searchhead">
      <el-select
        v-model="queryInfo.isInstructor"
        placeholder="是否仅看指导老师"
        @change="getTeacherListByPaging"
      >
        <el-option
          v-for="item in isInstructorOption"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input
        placeholder="搜索（支持工号、姓名）"
        v-model="queryInfo.keyword"
        class="input-with-select"
      >
        <el-button
          @click="getTeacherListByPaging"
          slot="append"
          icon="el-icon-search"
        ></el-button>
      </el-input>
      <el-button
        type="primary"
        plain
        style="margin-left: 10px"
        @click="addTeacher"
        >添加教师信息</el-button
      >
    </div>
    <el-table
      :data="teaList"
      border
      stripe
      max-height="600"
      style="width: 100%"
    >
      <el-table-column type="index" label="#" align="center"></el-table-column>
      <el-table-column
        prop="teacherId"
        label="工号"
        min-width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="teacherName"
        label="姓名"
        min-width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="mobile"
        label="电话"
        min-width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="qq"
        label="QQ"
        min-width="150"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="150" align="center">
        <template slot-scope="scope">
          <!--查看按钮-->
          <el-tooltip
            effect="dark"
            content="查看/编辑"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="primary"
              icon="el-icon-s-data"
              size="mini"
              plain
              @click="editTeacher(scope.row)"
            >
            </el-button>
          </el-tooltip>
          <!--删除按钮-->
          <el-tooltip
            effect="dark"
            content="删除"
            placement="top"
            :enterable="false"
          >
            <el-button
              @click="removeTeacherer(scope.row.teacherId)"
              type="danger"
              icon="el-icon-delete"
              plain
              size="mini"
            >
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[2, 5, 10, 30, 50, 100]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalPage"
    >
    </el-pagination>

    <!-- 查看、编辑教师信息的Dialog -->
    <el-dialog
      :visible.sync="teacherDialogShow"
      title="教师信息"
      width="40%"
      destroy-on-close
      :modal-append-to-body="false"
      :before-close="handleDialogClose"
      :close-on-click-modal.sync="teacherDialogReadOnly"
    >
      <el-form ref="teacher" :model="editingTeacher" label-width="85px">
        <el-form-item label="姓名" prop="teacherName">
          <el-input
            :disabled="teacherDialogReadOnly"
            v-model="editingTeacher.teacherName"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="工号" prop="teacherId">
          <el-input
            disabled
            v-model="editingTeacher.teacherId"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input
            :disabled="teacherDialogReadOnly"
            v-model="editingTeacher.mobile"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="QQ" prop="email">
          <el-input
            :disabled="teacherDialogReadOnly"
            v-model="editingTeacher.qq"
          >
          </el-input>
        </el-form-item>
        <div class="submitForm">
          <el-button type="info" plain @click="handleEditButton"
            ><span v-show="teacherDialogReadOnly">编辑</span>
            <span v-show="!teacherDialogReadOnly">取消</span>
          </el-button>
          <el-button type="primary" plain @click="submitEditTeacher"
            >提交</el-button
          >
        </div>
      </el-form>
    </el-dialog>
    <!-- 添加教师信息的Dialog -->
    <el-dialog
      :visible.sync="addTeacherDialogShow"
      title="教师信息"
      width="40%"
      destroy-on-close
      :modal-append-to-body="false"
      :close-on-click-modal="false"
    >
      <el-form ref="teacher" :model="editingTeacher" label-width="85px">
        <el-form-item label="姓名" prop="teacherName">
          <el-input v-model="editingTeacher.teacherName"> </el-input>
        </el-form-item>
        <el-form-item label="工号" prop="teacherId">
          <el-input type="number" v-model="editingTeacher.teacherId"> </el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="editingTeacher.mobile"> </el-input>
        </el-form-item>
        <el-form-item label="QQ" prop="email">
          <el-input v-model="editingTeacher.qq"> </el-input>
        </el-form-item>
        <div class="submitForm">
          <el-button type="info" plain @click="cancelAddTeacher">
            <span>取消</span>
          </el-button>
          <el-button type="primary" plain @click="submitAddTeacher"
            >提交</el-button
          >
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import ParamUtil from "../utils/paramsConvert";
export default {
  components: {},
  data() {
    return {
      teaList: [],
      editingTeacher: {
        teacherName: "",
        teacherId: "",
        mobile: "",
        qq: "",
      },
      // addTeacher: {},
      addTeacherDialogShow: false,
      queryInfo: {
        keyword: "",
        isInstructor: 0,
        pageNum: 1,
        pageSize: 10,
      },
      totalPage: 0,
      teacherDialogShow: false,
      teacherDialogReadOnly: true,
      isInstructorOption: [
        {
          value: 0,
          label: "查看所有老师",
        },
        {
          value: 1,
          label: "仅看指导老师",
        },
      ],
    };
  },
  created() {
    this.getAllTeacherList();
  },
  mounted() {},
  methods: {
    /**
     * 尝试从本地缓存拿所有老师的数据，如果没有则发起请求
     */
    async getAllTeacherList() {
      if (window.sessionStorage.getItem("teaList") != null) {
        this.teaList = JSON.parse(window.sessionStorage.getItem("teaList"));
        console.log("已从本地取得TeaList！", this.teaList);
      } else {
        this.getTeacherListByPaging();
      }
    },
    /**
     * 通过筛选条件获取老师列表
     */
    async getTeacherListByPaging() {
      let params = ParamUtil.excludeNull(this.queryInfo);
      const res = await this.$axios.post(
        `${this.HOST}/teacher/api/queryTeacher`,
        params
      );
      let data = res.data.data.teachers;
      this.teaList = data.list;
      this.totalPage = data.total;
      console.log("教师列表获取成功（列表）", this.teaList);
    },
    /**
     * 打开查看/编辑老师的窗口
     */
    editTeacher(teacher) {
      this.teacherDialogShow = true;
      this.editingTeacher = Object.assign({}, teacher); // 拷贝对象，防止引用
      //   this.editingTeacher = teacher;
    },
    /**
     * 根据id删除老师
     */
    removeTeacherer(id) {
      this.$confirm("此操作将永久删除该教师信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const res = await this.$axios.get(
          `${this.HOST}/teacher/api/deleteTeacher/` + id
        );
        if (res.data.meta.code == 200) {
          this.$message.success("删除成功！");
          this.getTeacherListByPaging();
        }
      });
    },
    /**
     * 监听pagesiez的改变
     * newSiez 当前的每一页的size
     */
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getTeacherListByPaging();
    },
    /**
     * 监听页码值变化
     * newPage 当前页
     */
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getTeacherListByPaging();
    },
    /**
     * 查看教师信息框的关闭
     */
    handleDialogClose(done) {
      this.teacherDialogReadOnly = true;
      return done();
    },
    /**
     * 处理信息框的 编辑/取消 的切换
     */
    handleEditButton() {
      this.teacherDialogReadOnly = !this.teacherDialogReadOnly;
      if (this.teacherDialogReadOnly) {
        this.teacherDialogShow = false;
        this.teacherDialogReadOnly = true;
      }
    },
    /**
     * 提交修改老师信息
     */
    async submitEditTeacher() {
      let params = ParamUtil.excludeNull(this.editingTeacher);
      const res = await this.$axios.post(
        `${this.HOST}/teacher/api/addTeacher`,
        params
      );
      if (res.data.meta.code == 200) {
        this.$message.success("添加成功！");
        this.editingTeacher = {};
        this.addTeacherDialogShow = false;
      }
      // console.log(params);
    },
    /**
     * 添加教师信息
     */
    async addTeacher() {
      this.editingTeacher = {};
      this.addTeacherDialogShow = true;
    },
    /**
     * 添加教师信息窗口的取消按钮动作
     */
    cancelAddTeacher() {
      this.addTeacherDialogShow = false;
      this.editingTeacher = "";
    },
    /**
     * 上传添加教师的Dialog
     */
    async submitAddTeacher() {
      let params = ParamUtil.excludeNull(this.editingTeacher);
      const res = await this.$axios.post(
        `${this.HOST}/teacher/api/addTeacher`,
        params
      );
      if (res.data.meta.code == 200) {
        console.log("添加教师成功！", params);
        this.$message.success("添加成功！");
        this.getTeacherListByPaging();
        this.addTeacherDialogShow = false;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  padding-left: 30px;
  padding-right: 30px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .searchhead {
    display: flex;

    .input-with-select {
      width: 300px;
      border-radius: 5px;
      margin-left: 10px;
    }
  }

  .submitForm {
    display: flex;
    justify-content: center;
  }
}
</style>